<?php

namespace common\components\office\grid;

use Yii;
use Closure;
use yii\helpers\ArrayHelper;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

/**
 * SelectColumn is the default column type for the [[GridView]] widget.
 *
 * A simple data column definition refers to an attribute in the data model of the
 * GridView's data provider. The name of the attribute is specified by [[attribute]].
 *
 * By setting [[value]] and [[label]], the header and cell content can be customized.
 *
 * A data column differentiates between the [[getDataCellValue|data cell value]] and the
 * [[renderDataCellContent|data cell content]]. The cell value is an un-formatted value that
 * may be used for calculation, while the actual cell content is a [[format|formatted]] version of that
 * value which may contain HTML markup.
 *
 * For more details and usage information on DataColumn, see the [guide article on data widgets](guide:output-data-widgets).
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class SelectColumn extends DataColumn {

    /**
     * @var string|Closure an anonymous function or a string that is used to determine the value to display in the current column.
     *
     * If this is an anonymous function, it will be called for each row and the return value will be used as the value to
     * display for every data model. The signature of this function should be: `function ($model, $key, $index, $column)`.
     * Where `$model`, `$key`, and `$index` refer to the model, key and index of the row currently being rendered
     * and `$column` is a reference to the [[DataColumn]] object.
     *
     * If this is not set, `$model[$attribute]` will be used to obtain the value, where `$attribute` is the value of [[attribute]].
     */
    public $dropdownList = [];
    public $dataValidationType = DataValidation::TYPE_LIST;

    /**
     * Returns the data cell value.
     * @param mixed $model the data model
     * @param mixed $key the key associated with the data model
     * @param int $index the zero-based index of the data model among the models array returned by [[GridView::dataProvider]].
     * @return string the data cell value
     */
    public function getDataCellValue($model, $key, $index) {
        $value = parent::getDataCellValue($model, $key, $index);
        $content = ArrayHelper::getValue($this->dropdownList, $value);
        if ($content === null) {
            $content = ArrayHelper::getValue($this->dropdownList, 0);
        }
        return $content;
    }

    public function getSheetTitle() {
        return $this->label;
    }

    public function getDataFormula() {
        $begin = 2;
        $end = count($this->dropdownList) + $begin - 1;
        return $this->getSheetTitle() . '!$A$' . $begin . ':$A$' . $end;
    }

    /**
     * {@inheritdoc}
     */
    public function setColumnCellDataValidation($spreadsheet) {
        if (empty($this->dropdownList)) {
            return;
        }
        $items = $this->dropdownList;
        $dataValidationSheet = $spreadsheet->createSheet()->setTitle($this->getSheetTitle());
        $i = 1;
        $dataValidationSheet->setCellValueByColumnAndRow(1, $i, $this->label);
        foreach ($items as $value) {
            $i++;
            $dataValidationSheet->setCellValueByColumnAndRow(1, $i, $value);
        }
        $dataValidationSheet->setSheetState(Worksheet::SHEETSTATE_VERYHIDDEN);
        $dataValidationSheet->getProtection()->setSheet(true);
    }

    /**
     * {@inheritdoc}
     */
    public function setCellDataValidation($cell) {
        if (empty($this->dropdownList)) {
            return;
        }
        //这一句为要设置数据有效性的单元格
        $dataValidation = $cell->getDataValidation();
        $dataValidation->setType($this->dataValidationType)
        ->setShowInputMessage(true)
        ->setShowErrorMessage(true)
        ->setAllowBlank(false)
        ->setShowDropDown(true);
//        $dataValidation->setFormula1('"' . $dropdownList . '"');
        $dataValidation->setFormula1($this->getDataFormula());
        $dataValidation->setErrorStyle(DataValidation::STYLE_STOP)
        ->setErrorTitle('无效输入')
        ->setError('请选择列表中的内容项');
    }

}
